package com.example.work2;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;

public class MyService extends Service {
    private MediaPlayer mediaPlayer;
    private Mybinder binder;

    @Override
    public void onDestroy() {
        Log.d("生命周期状态：","OnDestory");
        super.onDestroy();
        mediaPlayer.stop();
    }

    @Override
    public void onCreate() {
        Log.d("生命周期状态：","onCreate");
        super.onCreate();
        //初始化时就创建一个mediaplayer进行资源链接
        mediaPlayer=MediaPlayer.create(getApplicationContext(),R.raw.music);//加载音频文件
        mediaPlayer.start();
    }

    @Override
    //这是一个binder类
    public IBinder onBind(Intent intent) {
        binder=new Mybinder();
        return binder;//返回一个Mybinder的实例
    }

    class Mybinder extends Binder{
        public void myplay()
        {
            Log.d("音乐播放状态：","音乐播放成功！！！");
            mediaPlayer=MediaPlayer.create(getApplicationContext(),R.raw.music);//加载音频文件
            mediaPlayer.start();
        }
    }
}